package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.net.PostDele;
import longcaisuyun.longcai.com.net.PostTakeMoney;
import longcaisuyun.longcai.com.net.PostYinHangList;
import longcaisuyun.longcai.com.suyunbean.CardBean;
import longcaisuyun.longcai.com.utils.InputMoneyUtils;


public class TiXianActivity extends Activity implements View.OnClickListener {

    Button b_tx;
    private Dialog dialog;
    TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6;
    int num = 0;
    List<TextView> list = new ArrayList<TextView>();
    TextView et_tixian;
    ListView listcard;
    boolean type = true;
    List<CardBean> list_1 = new ArrayList();
    LinearLayout fanhui;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian);
        View view = getWindow().getDecorView();
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        fanhui = (LinearLayout) findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ViewInit();
        DataInit();

    }

    private void DataInit() {

        MyApplication.myviewutil.startProgressDialog(TiXianActivity.this);
        new PostYinHangList(MyApplication.myPreferences.readUid(), new AsyCallBack<PostYinHangList.Info>() {
            @Override
            public void onSuccess(String toast, int type, PostYinHangList.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("1")) {
                    listcard.setAdapter(new CardAdapter(TiXianActivity.this, info.list));
                    list_1 = info.list;
                } else {
                    Toast.makeText(TiXianActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                Toast.makeText(TiXianActivity.this, "网络故障，获取数据失败", Toast.LENGTH_SHORT).show();
                MyApplication.myviewutil.stopProgressDialog();
            }
        }).execute(TiXianActivity.this);
    }

    private void ViewInit() {
        et_tixian = (TextView) findViewById(R.id.et_tixian);
        InputFilter[] inputFilters={new InputMoneyUtils()};
        et_tixian.setFilters(inputFilters);
        b_tx = (Button) findViewById(R.id.b_tx);
        listcard = (ListView) findViewById(R.id.listcard);
        listcard.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                dialg();
                type = false;
                return false;
            }
        });
        b_tx.setOnClickListener(this);
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.b_tx:
                if (!wh.utils.StringUtils.isEmpty(et_tixian.getText().toString().trim())) {
                    dialg();
                    type = true;
                } else {
                    Toast.makeText(TiXianActivity.this, "请输入提现金额", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.b_1:
                add("1");

                break;
            case R.id.b_2:
                add("2");
                break;
            case R.id.b_3:
                add("3");
                break;
            case R.id.b_4:
                add("4");
                break;
            case R.id.b_5:
                add("5");
                break;
            case R.id.b_6:
                add("6");
                break;
            case R.id.b_7:
                add("7");
                break;
            case R.id.b_8:
                add("8");
                break;
            case R.id.b_9:
                add("9");
                break;
            case R.id.b_0:
                add("0");
                break;
            case R.id.b_del:
                del();
                break;
            case R.id.fanhui_lin:
                type = false;
                dialog.dismiss();

                break;
            case R.id.t_wangji:
                startActivity(new Intent(TiXianActivity.this, WangJiActivity.class));
                dialog.dismiss();
                break;
            case R.id.t_xiugai:
                startActivity(new Intent(TiXianActivity.this, LiShiActivity.class));
                dialog.dismiss();
                break;
        }
    }

    public String getpassword() {
        String s = "";
        for (int j = 0; j < list.size(); j++) {
            s += list.get(j).getText().toString();
        }
        return s;
    }

    public void add(String j) {

        if (num < 6) {
            list.get(num).setText(j);
            if (num < 6)
                num++;
        }
        if (num == 6) {
            if (type) {
                MyApplication.myviewutil.startProgressDialog(TiXianActivity.this);
                new PostTakeMoney(MyApplication.myPreferences.readUid(), et_tixian.getText().toString(), getpassword(), list_1.get(0).getId(), new AsyCallBack<PostTakeMoney.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, PostTakeMoney.Info info) throws Exception {
                        super.onSuccess(toast, type, info);
                        if (info.message.equals("0")) {
                            Toast.makeText(TiXianActivity.this, "提现失败", Toast.LENGTH_SHORT).show();
                        }
                        if (info.message.equals("1")) {
                            Toast.makeText(TiXianActivity.this, "提现成功", Toast.LENGTH_SHORT).show();
                            Intent i = new Intent();
                            i.setClass(TiXianActivity.this, TiXianResultActivity.class);
                            i.putExtra("name", list_1.get(0).getName());
                            i.putExtra("num", subString(list_1.get(0).getBankcard()));
                            i.putExtra("money", et_tixian.getText().toString());
                            startActivity(i);
                            finish();
                        }
                        if (info.message.equals("2")) {
                            Toast.makeText(TiXianActivity.this, "余额不足", Toast.LENGTH_SHORT).show();
                        }
                        if (info.message.equals("3")) {
                            Toast.makeText(TiXianActivity.this, "支付密码不正确", Toast.LENGTH_SHORT).show();
                        }
                        if (info.message.equals("4")) {
                            Toast.makeText(TiXianActivity.this, "支付密码没有设置", Toast.LENGTH_SHORT).show();
                        }
                        if (info.message.equals("5")) {
                            Toast.makeText(TiXianActivity.this, "银行卡没有设置", Toast.LENGTH_SHORT).show();
                        }
                        dialog.dismiss();
                        MyApplication.myviewutil.stopProgressDialog();

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                        dialog.dismiss();
                        MyApplication.myviewutil.stopProgressDialog();
                        Toast.makeText(TiXianActivity.this, "网络故障,请稍后重试", Toast.LENGTH_SHORT).show();

                    }
                }).execute(TiXianActivity.this);

            } else {
                MyApplication.myviewutil.startProgressDialog(TiXianActivity.this);
                new PostDele(MyApplication.myPreferences.readUid(), getpassword(), new AsyCallBack<PostDele.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, PostDele.Info info) throws Exception {
                        super.onSuccess(toast, type, info);
                        if (info.message.equals("0")) {
                            Toast.makeText(TiXianActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                        } else if (info.message.equals("1")){
                            Toast.makeText(TiXianActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }else if (info.message.equals("2")){
                            Toast.makeText(TiXianActivity.this, "密码错误,删除失败", Toast.LENGTH_SHORT).show();
                        }
                        dialog.dismiss();
                        MyApplication.myviewutil.stopProgressDialog();
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                        MyApplication.myviewutil.stopProgressDialog();
                        Toast.makeText(TiXianActivity.this, "网络故障,请稍后重试", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                }).execute(TiXianActivity.this);
            }
        }
    }

    public void del() {
        Log.e("num", num + "");
        if (num == 6) {
            num = 5;
            list.get(num).setText("");
        } else {
            list.get(num).setText("");
        }

        if (num > 0) num--;
    }

    public void dialg() {
        LayoutInflater inflater = LayoutInflater.from(TiXianActivity.this);
        View layout =  inflater.inflate(
                R.layout.item_tixian, null);

        dialog = new Dialog(TiXianActivity.this,R.style.MyDialog);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                num = 0;
                list.clear();
                type = false;
            }
        });
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mypopwindow_anim_style);
        dialog.show();

        WindowManager windowManager = this.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); // 设置宽度
        dialog.getWindow().setAttributes(lp);

        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog.setContentView(layout);


        tv_1 = (TextView) layout.findViewById(R.id.tv_1);
        tv_2 = (TextView) layout.findViewById(R.id.tv_2);
        tv_3 = (TextView) layout.findViewById(R.id.tv_3);
        tv_4 = (TextView) layout.findViewById(R.id.tv_4);
        tv_5 = (TextView) layout.findViewById(R.id.tv_5);
        tv_6 = (TextView) layout.findViewById(R.id.tv_6);
        list.add(tv_1);
        list.add(tv_2);
        list.add(tv_3);
        list.add(tv_4);
        list.add(tv_5);
        list.add(tv_6);
        Button b_1 = (Button) layout.findViewById(R.id.b_1);
        Button b_2 = (Button) layout.findViewById(R.id.b_2);
        Button b_3 = (Button) layout.findViewById(R.id.b_3);
        Button b_4 = (Button) layout.findViewById(R.id.b_4);
        Button b_5 = (Button) layout.findViewById(R.id.b_5);
        Button b_6 = (Button) layout.findViewById(R.id.b_6);
        Button b_7 = (Button) layout.findViewById(R.id.b_7);
        Button b_8 = (Button) layout.findViewById(R.id.b_8);
        Button b_9 = (Button) layout.findViewById(R.id.b_9);
        Button b_0 = (Button) layout.findViewById(R.id.b_0);
        Button b_del = (Button) layout.findViewById(R.id.b_del);
        b_1.setOnClickListener(this);
        b_2.setOnClickListener(this);
        b_3.setOnClickListener(this);
        b_4.setOnClickListener(this);
        b_5.setOnClickListener(this);
        b_6.setOnClickListener(this);
        b_7.setOnClickListener(this);
        b_8.setOnClickListener(this);
        b_9.setOnClickListener(this);
        b_0.setOnClickListener(this);
        b_del.setOnClickListener(this);
        LinearLayout fanhui_lin = (LinearLayout) layout.findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(this);
        TextView t_wangji = (TextView) layout.findViewById(R.id.t_wangji);
        t_wangji.setOnClickListener(this);
        TextView t_xiugai = (TextView) layout.findViewById(R.id.t_xiugai);
        t_xiugai.setOnClickListener(this);
    }

    public String subString(String str) {

        return "尾号" + str.substring(str.length() - 4, str.length());
    }
}